package de.baumann.browser.present.explore

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.AccountInfo
import de.baumann.browser.api.net.vo.ExploreMode
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IExploreViews
import de.baumann.browser.model.ExploreApiMode
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.*

class ExplorePresenters : BasePresenter<IExploreViews>() {
    private val userModel = UserModel()
    fun getCountDown() {
        ExploreApiMode()
                .getExplore().subscribe(object : OdinObserver<ExploreMode>() {
                    override fun onSuccess(data: ExploreMode) {
                        if (data != null && data.configValue != "") {
                            view.setCountDown(data.remark)
                            view.setSurplusPoser(data.configValue)
                        } else {
                            view.setSurplusPoser("0")
                        }
                    }
                })
    }

    fun getSumPower() {
        var moneyAddress = getMoneyAddress()
        if (moneyAddress?.startsWith("0x") == true) {
            moneyAddress = "od" + moneyAddress?.substring(1)
        }
        ExploreApiMode()
                .getExploreOdinSum(moneyAddress!!, "2021-07-01", "2021-07-30").subscribe(object : OdinObserver<String>() {
                    override fun onSuccess(data: String) {
                        getSumPower(data)
                    }
                })
    }

    fun getSumPower(power: String) {
        var moneyAddress = getMoneyAddress()
        if (moneyAddress?.startsWith("0x") == true) {
            moneyAddress = "od" + moneyAddress?.substring(1)
        }
        ExploreApiMode()
                .getExploreOtherSum(moneyAddress!!, "2021-07-01", "2021-07-30").subscribe(object : OdinObserver<String>() {
                    override fun onSuccess(data: String) {
                        view.setPoserSum(power.toInt() + data.toInt())
                    }
                })
    }

    fun login() {
        userModel.login(getMoneyAddress(), "")
                .subscribe(object : OdinObserver<LoginInfo>(view) {
                    override fun onSuccess(loginInfo: LoginInfo) {
                        saveUser(loginInfo)
                        UnicornUtil.setUserInfo(loginInfo)
                        saveInviteCode(loginInfo.inviteCode)
                        saveInviteCodeStatus(loginInfo.inviteId)
                        saveFormAddress(loginInfo.fromAddress)
                        saveLogin(true)
                    }
                })
    }
}